package edu.nctu.csie.jichang.dp.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class GuardedSuspend {
	public static void main(String[] args)throws Exception {
		final RequestQueue tQueue = new RequestQueue();
		new Thread(){
			public void run() {
				try {sleep(1000);}catch (Exception e) {e.printStackTrace();}
				tQueue.putRequest(new Request());
			};
		}.start();
		tQueue.getRequest().doExecute();
		tQueue.getRequest().doExecute();
		tQueue.getRequest().doExecute();
	}
}
class Request{void doExecute(){System.out.println("Execute Request ... ");}}
class RequestQueue {
	BlockingQueue<Request> queue = new ArrayBlockingQueue<Request>(10);
	RequestQueue() {
		queue.add(new Request());
		queue.add(new Request());
	}
	synchronized Request getRequest()throws Exception{
		return queue.take();
	}
	void putRequest(Request request){
		queue.add(request);
	}
}